package com.xdf.xzymanagementsystem.controller;

import com.xdf.xzymanagementsystem.dto.JsonResult;
import com.xdf.xzymanagementsystem.dto.QueryParams;
import com.xdf.xzymanagementsystem.entity.BillType;
import com.xdf.xzymanagementsystem.entity.ResponseResult;
import com.xdf.xzymanagementsystem.enums.MenuConstant;
import com.xdf.xzymanagementsystem.enums.OperateConstant;
import com.xdf.xzymanagementsystem.enums.ResponseEnum;
import com.xdf.xzymanagementsystem.enums.SeparatorConstant;
import com.xdf.xzymanagementsystem.service.BillTypeService;
import com.xdf.xzymanagementsystem.util.Example;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description : 自动生成的控制层
 * @Author : 张柯
 * @Date : 2021-01-27 12:44:45
 */
@Api(tags = "单据类型")
@CrossOrigin
@RestController
@RequestMapping("/billType")
public class BillTypeController {

    @Autowired
    private BillTypeService service;

    /**
     * @Description : 选择新增
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2021-01-27 12:44:45
     */
    @ApiOperation(value = "选择新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequiresPermissions(MenuConstant.MENU406 + SeparatorConstant.SEPARATOR + OperateConstant.ADD)
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    public ResponseResult<Void> insertSelective(BillType record) throws Exception {
        service.insertSelective(record);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 选择修改
     * @Return : BillType
     * @Author : 张柯
     * @Date : 2021-01-27 12:44:45
     */
    @ApiOperation(value = "选择修改", httpMethod = "PUT", notes = "自动生成的修改方法")
    @RequiresPermissions(MenuConstant.MENU406 + SeparatorConstant.SEPARATOR + OperateConstant.MODIFY)
    @RequestMapping(value = "/updateSelective", method = RequestMethod.POST)
    public ResponseResult<Void> updateSelective(BillType record) throws Exception {
        service.updateByPrimaryKeySelective(record);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 根据主键查询
     * @Return : BillType
     * @Author : 张柯
     * @Date : 2021-01-27 12:44:45
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/queryByPk/{id}", method = RequestMethod.GET)
    public BillType selectByPrimaryKey(@PathVariable("id") String id) throws Exception {
        if (id == null) return null;
        return service.selectByPrimaryKey(id);
    }

    /**
     * @Description : 查询方法
     * @Return : java.util.List
     * @Author : 张柯
     * @Date : 2021-01-27 12:44:45
     */
    @ApiOperation(value = "查询方法", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/listQuery", method = RequestMethod.GET)
    public List<BillType> query(BillType record) throws Exception {
        return service.listQuery(record);
    }

    /**
     * @Description : 查询方法
     * @Return : java.util.List
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "查询方法", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/jsonQuery", method = RequestMethod.GET)
    public JsonResult<BillType> query(QueryParams params) throws Exception {
        if (params == null) return null;
        return JsonResult.<BillType>builder()
                .rows(service.listQuery(new Example().setRowSize(params.getRows()).setStartRow(params.getStartRow()).setRowSize(params.getRows())))
                .total(service.count(new Example()))
                .build();
    }

    /**
     * @Description : 根据主键删除方法
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2021-01-27 12:44:45
     */
    @ApiOperation(value = "根据主键删除方法", httpMethod = "DELETE", notes = "自动生成的删除方法")
    @RequiresPermissions(MenuConstant.MENU406 + SeparatorConstant.SEPARATOR + OperateConstant.DELETE)
    @RequestMapping(value = "/deleteByPk", method = RequestMethod.POST)
    public ResponseResult<Void> deleteByPrimaryKey(@RequestParam("id") String id) throws Exception {
        service.deleteByPrimaryKey(id);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * 根据仓库获取单据类型
     *
     * @param storage
     * @param accessMode
     * @param allowManual
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据条件查询单据类型", httpMethod = "GET", notes = "根据条件查询单据类型")
    @RequestMapping(value = "/selectBy/{storage}/{accessMode}")
    public List<BillType> selectBillTypeByParam(@PathVariable("storage") String storage, @PathVariable("accessMode") int accessMode,
                                                @RequestParam(value = "allowManual", required = false) Boolean allowManual) throws Exception {
        return service.getBillType(storage, accessMode, allowManual);
    }

    /**
     * 2021年8月1日 15:49:44 chanchaw
     * 今日无法生成发货单，制作下面接口救急，用于获取成品出库的单据类型
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据条件查询单据类型", httpMethod = "GET", notes = "根据条件查询单据类型")
    @GetMapping("/selectCPCK")
    public List<BillType> selectCPCK() throws Exception {
        BillType billType = service.selectByPrimaryKey("CPCK");
        List<BillType> list = new ArrayList<>();
        list.add(billType);
        return list;
    }

    @ApiOperation(value = "根据条件查询单据类型", httpMethod = "GET", notes = "根据条件查询单据类型")
    @RequestMapping(value = "/selectCGRK")
    public List<BillType> selectCGRK() throws Exception {
        BillType billType = service.selectByPrimaryKey("CGRK");
        List<BillType> list = new ArrayList<>();
        list.add(billType);
        return list;
    }

    @ApiOperation(value = "获取“材料入库单”.“单据类型”下拉数据", httpMethod = "GET", notes = "获取“材料入库单”.“单据类型”下拉数据")
    @RequestMapping(value = "/selectBillTypeMaterial")
    public List<BillType> selectBillTypeMaterial() throws Exception {
        return service.getBillTypeMaterial();
    }
}